import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
import VueAxios from 'vue-axios'
import echarts from 'echarts'
import VueRouter from 'vue-router'
import tagsViews from './components/my-tag.vue'
import Base64 from './util/Base64.js'
import Cookies from 'js-cookie'
import http from "./components/axios.js"
import Vue2Editor from "vue2-editor";

Vue.use(Vue2Editor);
Vue.use(Cookies)
// React.Component.prototype.$Cookie = Cookie;

Vue.prototype.$echarts = echarts
Vue.component('my-tag', tagsViews)

Vue.use(VueAxios, axios)
Vue.use(ElementUI);
Vue.config.productionTip = false;
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}
import { Message } from 'element-ui'

// 为了实现Class的私有属性
const showMessage = Symbol('showMessage')
/** 
 *  重写ElementUI的Message
 *  single默认值true，因为项目需求，默认只弹出一个，可以根据实际需要设置
 */
let messageInstance = null;
class DonMessage {
  success (options, single = true) {
    this[showMessage]('success', options, single)
  }
  warning (options, single = true) {
    this[showMessage]('warning', options, single)
  }
  info (options, single = true) {
    this[showMessage]('info', options, single)
  }
  error (options, single = true) {
    this[showMessage]('error', options, single)
  }
  [showMessage] (type, options, single) {
    if (messageInstance && single) {
      messageInstance.close()//先把原来的关闭
    }
    messageInstance = Message[type](options)//再创建新的消息
  }
}
Vue.prototype.$message = new DonMessage()
//路由守卫
router.beforeEach((to, from, next) => {
	let token = Cookies.get('token');
	// console.log(to.path != '/login' && token === undefined)
	if (to.path != '/login' && token === undefined) {
		next({
			path: '/login',
			query: {
				order: 'login'
			}
		});
	} else {
		next();
	}
});

 const permission={
     inserted(el, binding, vnode) {
         const { value } = binding
         const roles = ['editor','admin']
         if (value && value instanceof Array && value.length > 0) {
             const permissionRoles = value
             const hasPermission = roles.some(role => {
                 return permissionRoles.includes(role)
             })
             if (!hasPermission) {
                 el.parentNode && el.parentNode.removeChild(el)
             }
         } else {
             throw new Error(`need roles! Like v-permission="['admin','editor']"`)
         }
     }
 }
 
 Vue.directive('permission', permission)

new Vue({
	router,
	render: (h) => h(App),
}).$mount("#app");
